//
//  SLCircleProgress.m
//  SLPhotoBrowser
//
//  Created by S_LINE on 2020/6/21.
//  Copyright © 2020 S_LINE. All rights reserved.
//

#import "SLCircleProgress.h"

@interface SLCircleProgress ()

@property (nonatomic, strong) CAShapeLayer *indicatorLayer;

@end

@implementation SLCircleProgress

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self setupLayer];
    }
    return self;
}

- (void)setupLayer
{
    self.backgroundColor = [UIColor clearColor];
    self.layer.backgroundColor = [UIColor clearColor].CGColor;
}

- (void)drawRect:(CGRect)rect
{
    CGFloat width = rect.size.width;
    CGFloat radius = width / 2.0;
    CGFloat startAngle = -M_PI_2;
    CGFloat endAngle = startAngle;
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetFillColorWithColor(ctx, [UIColor clearColor].CGColor);
    CGContextSetLineWidth(ctx, 2.0f);
    endAngle = startAngle + self.progress * (M_PI * 2);
    CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
    CGContextAddArc(ctx, radius, radius, radius - 1, startAngle, endAngle, NO);
    CGContextStrokePath(ctx);
    CGContextSetStrokeColorWithColor(ctx, [[UIColor lightGrayColor] colorWithAlphaComponent:0.6].CGColor);
    CGContextAddArc(ctx, radius, radius, radius - 1, endAngle, startAngle, NO);
    CGContextStrokePath(ctx);
}

#pragma mark - setter
- (void)setProgress:(CGFloat)progress
{
    _progress = progress;
    
    [self setNeedsDisplay];
}

@end
